# Create your views here.
from django.shortcuts import render_to_response
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from django.http import HttpResponse

from books1.models import Computer,Customer
def main_page(request):#show ex.html by template
    return render_to_response('ex.html')
def inputs_add(request):
    cnt = 0
    if request.POST:
        cust_id = request.POST['cust_id']
        cust_name = request.POST['cust_name']
        com_id = request.POST['com_id']
        state = request.POST['state']
        rec_date = request.POST['rec_fix_date']
        fin_date=request.POST['fin_fix_date']
        re_date = request.POST['re_date']
        who = request.POST['who']    
        cnt=Computer.objects.filter(Com_ID=com_id).count()
        if(cnt>=1 or cust_id == '' or cust_name == '' or com_id == '' or state == '' or rec_date == '' or fin_date == '' or re_date == '' or who ==''):
            return HttpResponseRedirect('/showInput_wrong')
        else:
            save_database(cust_id,cust_name,com_id,state,rec_date,fin_date,re_date,who)
            return HttpResponseRedirect('/showInput_correct')
    else:
        return render_to_response('input_form.html', {'error': True})

def inputs_update(request):
    cnt1 = 0
    cnt2 = 0
    cnt3 = 0
    if request.POST:
        cust_id = request.POST['cust_id']
        cust_name = request.POST['cust_name']
        com_id = request.POST['com_id']
        state = request.POST['state']
        rec_date = request.POST['rec_fix_date']
        fin_date=request.POST['fin_fix_date']
        re_date = request.POST['re_date']
        who = request.POST['who']    
        cnt1=Computer.objects.filter(Com_ID=com_id).count()
        cnt2=Customer.objects.filter(Cust_Name=cust_name).count()
        cnt3=Customer.objects.filter(Cust_ID=cust_id).count()
        if(cnt1==0 and cnt2==0 and cnt3==0 ):
            return HttpResponseRedirect('/update_wrong')
        else:
            update(cust_id,cust_name,com_id,state,rec_date,fin_date,re_date,who)
            return HttpResponseRedirect('/showInput_correct')
    else:
        return render_to_response('update_form.html', {'error': True})
    
def save_database(cust_id,cust_name,com_id,state,rec_date,fin_date,re_date,who):
    Customer.objects.create(Cust_ID=cust_id,Cust_Name=cust_name)
    Computer.objects.create(Cust_ID=cust_id,Com_ID=com_id,State=state,Recieve_fix_date=rec_date,Finish_fix_date=fin_date,Return_date=re_date,Who=who)
    return 0

def computer_list(request):
    computer = Computer.objects.all()
    customer = Customer.objects.all()
    return render_to_response('computer.html', locals())

def show_input_wrong(request):
    return render_to_response('input_form_again.html')

def update_wrong(request):
    return render_to_response('update_form_again.html')

def show_input_correct(request):
    numbers_computer = Computer.objects.order_by("Com_ID")
    numbers_customer = Customer.objects.order_by("Cust_ID")
    count = 0
    for com in numbers_computer:
        count = count+1
    computer=Computer.objects.all()[count-1]
    count = 0
    for com in numbers_customer:
        count = count+1
    customer=Customer.objects.all()[count-1]
    return render_to_response('input_results.html',locals())

def show_state(request):
    st_good=Computer.objects.filter(State="GOOD")
    st_bad=Computer.objects.filter(State="BAD")
    count=0
    good=0
    bad = 0
    for count in range(len(st_good)):
        count=count+1
    good=count
    count=0
    for count in range(len(st_bad)):
        count=count+1
    bad=count
    scale = []
    gp_good= 448-(good*30)
    gp_bad= 448-(bad*30)
    for i in range(0,5):
        scale.append(390-(i*60))
    return render_to_response('show_state.html',locals())
def update(cust_id,cust_name,com_id,state,rec_date,fin_date,re_date,who):       
    Customer.objects.filter(Cust_ID=cust_id).delete()
    Computer.objects.filter(Com_ID=com_id).delete()
    save_database(cust_id,cust_name,com_id,state,rec_date,fin_date,re_date,who)
